/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.webservice.client;

import com.edu.kamai.client.control.usrControl;
import com.edu.kamai.webservice.user.User;
import com.edu.kamai.webservice.user.UserAccountService;
import com.edu.kamai.webservice.user.UserAccountService_Service;
import java.net.URL;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.ws.soap.SOAPFaultException;

/**
 *
 * @author Administrator
 */
public class WSUserAccountClient {

    private final QName qName = new QName("http://webservice.kamai.edu.com/","userAccountService");
    private URL[] url;
    private int size;
    
    public WSUserAccountClient() throws Exception{
        size = usrControl.getNetList().size();
        url = new URL[size];
        for(int i=0;i<size;i++){
            url[i] = new URL("http://" + usrControl.getNetList().get(i) + "/userAccountService/userAccountService?WSDL");
        }
    }
    
    public int register(String UsrID,String UsrPwd,String UsrEmail,int role) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        int res = 0;
        do{
            try{
                System.out.println("register : " + url[k]);
                UserAccountService_Service service = new UserAccountService_Service(url[k], qName);
                UserAccountService port = service.getUserAccountServicePort();
                res = port.register1(UsrID, UsrPwd, UsrEmail, role);
                find = true;
                break;
            }catch(SOAPFaultException e1){
                e1.printStackTrace();
                ex = e1;
                break;
            }catch(Exception e){
               e.printStackTrace();
               ex = e;
               k=(k+1)%size;
               break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return res;
    }
    
    public int login(String usrID,String usrPwd,int role) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        int res = 0;
        do{
            try{
                System.out.println("login : " + url[k]);
                UserAccountService_Service service = new UserAccountService_Service(url[k], qName);
                UserAccountService port = service.getUserAccountServicePort();
                res = port.login(usrID, usrPwd, role);
                find = true;
            }catch(Exception e){
                k=(k+1)%size;
                ex = e;
                break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return res;
    }
    
    public int validateUsr(String usrID) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        int res = 0;
        do{
            try{
                System.out.println("validate user : " + url[k]);
                UserAccountService_Service service = new UserAccountService_Service(url[k], qName);
                UserAccountService port = service.getUserAccountServicePort();
                res = port.validateUsr(usrID);
                find = true;
            }catch(Exception e){
                k=(k+1)%size;
                ex = e;
                break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return res;
    }
    
    public List<User> chekPendingList() throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        int res = 0;
        List<User> pending = null;
        do{
            try{
                System.out.println("chekPendingList : " + url[k]);
                UserAccountService_Service service = new UserAccountService_Service(url[k], qName);
                UserAccountService port = service.getUserAccountServicePort();
                pending = port.chekPendingList();
                find = true;
            }catch(Exception e){
                k=(k+1)%size;
                ex = e;
                break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return pending;
    }
    
 
    
}
